﻿package com.wandian.util.loader
{
	import com.wandian.data.GameData;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;

	public class ImageLoader extends EventDispatcher
	{
		private var loader:Loader;
		private var _bitmapData:BitmapData;
		private var _w:Number;
		private var _h:Number;
		private var _personId:int;
		private var _url:String="";
		;

		public function ImageLoader()
		{
			return;
		}

		public function load(url:String, numId:int=0, _width:Number=0, _height:Number=0):void
		{
			this._w=_width;
			this._h=_height;
			this._personId=numId;
			this.loader=new Loader();
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.imageDataComplete);
			this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
			this.loader.load(new URLRequest(url));
			_url=url;
			return;
		}

		public function loadBytes(bytes:ByteArray):void
		{
			this.loader=new Loader();
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.imageDataComplete);
			this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
			this.loader.loadBytes(bytes);
			return;
		}

		private function onError(event:IOErrorEvent):void
		{
			trace("error " + _url);
			return;
		}

		private function imageDataComplete(event:Event):void
		{
			trace("ok " + _url);
			var content:Bitmap=event.target.content as Bitmap;
			this._bitmapData=new BitmapData(content.width, content.height, true, 0);
			this._bitmapData.draw(content);
			dispatchEvent(event);
			return;
		}

		public function get personId():int
		{
			return this._personId;
		}

		public function get bitmapdata():BitmapData
		{
			return this._bitmapData;
		}

	}
}
